package com.infocollect.project.api.service.impl;


import com.infocollect.project.api.entity.TappUserEntity;
import com.infocollect.project.api.helper.JSONUtil;
import com.infocollect.project.api.service.ITappRoleService;
import com.infocollect.project.api.service.ITappUserService;
import com.infocollect.project.api.vo.ApprovalParam;
import com.infocollect.project.api.vo.ProcInstHandleEntity;
import com.infocollect.project.api.vo.RoleUserVO;

import java.util.List;

public class ApproverParsing {
    private ProcInstHandleEntity ProcInst;
    FlowCommon flowCommon;
    ITappUserService tappUserDao;
    ITappRoleService tappRoleDao;
    public ApproverParsing(ProcInstHandleEntity procinst , ITappUserService _tappUserDao, ITappRoleService _tappRoleDao) {
        ProcInst = procinst;
        flowCommon=new FlowCommon(procinst);
        tappUserDao=_tappUserDao;
        tappRoleDao=_tappRoleDao;
    }

    public String GetNextActApprover() throws Exception {
        String ApprovalType = ProcInst.getNexact().getFapprovaltype();
        String ApprovalName = ProcInst.getNexact().getFapprovername();
        String approver = ProcInst.getNexact().getFapprover();
        switch (ApprovalType) {
            //等于 需要判断是否是角色，人员，部门 获取
            case "1":
                String ApprovalerStr="";
                List<ApprovalParam>  approvalParamList= JSONUtil.jsonToObjList(ApprovalParam.class,approver);
                for (ApprovalParam item:approvalParamList) {
                   switch (item.getType()){
                       case "UserNO":ApprovalerStr=ApprovalerStr==""?item.getNo():ApprovalerStr+","+item.getNo(); break;
                       case "UnitNO":
                           String deptUserStr=GetDeptCodeUser(item.getNo());
                           if(deptUserStr!=""){
                               ApprovalerStr=ApprovalerStr==""?deptUserStr:ApprovalerStr+","+deptUserStr;
                           }
                           break;
                       case "CustomRole":
                           String roleUserStr=GetRoleUser(Long.parseLong(item.getNo()));
                           if(roleUserStr!=""){
                               ApprovalerStr=ApprovalerStr==""?roleUserStr:ApprovalerStr+","+roleUserStr;
                           }
                           break;
                   }
                }
                return ApprovalerStr;
            case "2":
                if (ApprovalName == null || ApprovalName.length() == 0) {
                    throw new Exception("获取审批人失败!");
                }
                String Approvaler=flowCommon.controlVlaue(approver);
                if(Approvaler==null||Approvaler.length()==0){
                    throw new Exception("未找到自定义【"+ApprovalName+"】控件审批人！");
                }
                return Approvaler;
        }
      return null;
    }
    ///获取角色成员
    public  String GetRoleUser(long roleid){
        RoleUserVO roleUser=new RoleUserVO();
        roleUser.setFroleid(roleid);
        List<RoleUserVO> roleUserList= tappRoleDao.getUserByRole(roleUser);
        String userstr="";
        for (RoleUserVO useritem:roleUserList) {
            userstr=userstr==""? useritem.getCode():userstr+","+useritem.getCode();
        }
        return userstr;
    }
    ///根据部门ID 获取所有部门下的用户
    public  String GetDeptCodeUser(String deptcode){
        TappUserEntity tappUser=new TappUserEntity();
        tappUser.setFdeptcode(deptcode);
        List<TappUserEntity> tappUserList= tappUserDao.queryAll(tappUser);
        String userstr="";
        for (TappUserEntity useritem:tappUserList) {
            userstr=userstr==""? useritem.getFusername():userstr+","+useritem.getFusername();
        }
        return userstr;
    }

}
